package com.traffic.between.service;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.Xml2JsonUtil;


public class YiSaiServiceImpl {
	private static Logger logger = Logger.getLogger(YiSaiServiceImpl.class);
	public static YiSaiServiceImpl service = new YiSaiServiceImpl();
	
	private static String HOST_ORDER_PATH = BetweenProperties.YISAI_ORDER_URL;
	private static String YISAI_USERSYSTEMKEY = BetweenProperties.YISAI_USERSYSTEMKEY;
	//private static String YISAI_USERCUSTOMKEY = BetweenProperties.YISAI_USERCUSTOMKEY;
	private static String YISAI_USERNUMBER = BetweenProperties.YISAI_USERNUMBER;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.YISAI_ORDERSTATUS_URL;
	
	

	public String order(String productid, String mobile, String transNo,String back_url) {
	
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date); 
		String UserNumber = YISAI_USERNUMBER;
	
		int PayAmount = 1;// 固定值
	 
		int TimeOut = 3600;
		String StartTime = time;
		//String Remark = "";
	   
		String UserSystemKey = YISAI_USERSYSTEMKEY;
		//String UserCustomKey = YISAI_USERCUSTOMKEY;
		
 
        String RecordKey = getMD5(UserNumber+transNo+productid+mobile+PayAmount+StartTime+TimeOut+UserSystemKey);
        
        String result = "";
        
       
	
        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("UserNumber", UserNumber));
        pairList.add(new BasicNameValuePair("OutOrderNumber", transNo));
        pairList.add(new BasicNameValuePair("ProId",productid));
        pairList.add(new BasicNameValuePair("PhoneNumber", mobile));
        pairList.add(new BasicNameValuePair("PayAmount", PayAmount+""));
        pairList.add(new BasicNameValuePair("StartTime", StartTime));
        pairList.add(new BasicNameValuePair("TimeOut", TimeOut+""));
        pairList.add(new BasicNameValuePair("CallBackUrl", back_url));
        pairList.add(new BasicNameValuePair("RecordKey", RecordKey));
       
		HttpPost httpPost = new HttpPost(HOST_ORDER_PATH);
		CloseableHttpClient client = HttpClients.createDefault();
		logger.info("between- 南宁易赛流量订购请求时的参数: " + pairList);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			logger.info("between- 南宁易赛流量订购请求时的响应statusCode: " + statusCode);
			
			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = Xml2JsonUtil.xmlToJson(EntityUtils.toString(entity, "utf-8")) ;
				logger.info("between- 南宁易赛订购请求时的响应数据是: " + result);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}

	
		return result;
	}

	
	
	
	
	
	
	public String orderStatus(String cstmOrderNo) {

		logger.info("between- 南宁易赛订购状态请求参数: params cstmOrderNo = " + cstmOrderNo);
		String UserNumber = YISAI_USERNUMBER;
		String UserSystemKey = YISAI_USERSYSTEMKEY;
		//String UserCustomKey = YISAI_USERCUSTOMKEY;
		String RecordKey =getMD5(UserNumber+cstmOrderNo+UserSystemKey).substring(0,16);
		
	  
	    String result = "";
	    List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("UserNumber", UserNumber));
        pairList.add(new BasicNameValuePair("OutOrderNumber", cstmOrderNo));
        pairList.add(new BasicNameValuePair("RecordKey", RecordKey));
       
        
    	HttpPost httpPost = new HttpPost(HOST_ORDERSTATUS_PATH);
		CloseableHttpClient client = HttpClients.createDefault();
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			logger.info("between-南宁易赛流量订购请求时的响应statusCode: " + statusCode);
			
			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = Xml2JsonUtil.xmlToJson(EntityUtils.toString(entity, "utf-8"));
				logger.info("between- 南宁易赛流量订购请求时的响应数据是: " + result);
			}
		} catch (Exception e) {
			logger.info("between- 南宁易赛流量订购请求时異常" + e.getMessage());
		}
		return result;
	}

	
	/**
	 * md5加密 ，用于生成sign
	 * 
	 * @param message
	 * @return
	 */
	public static String getMD5(String message) {
		MessageDigest md5;
		String result = "";
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.update(message.getBytes("utf-8"));
			
		
			byte[] temp;
			temp = md5.digest();
			for (int i = 0; i < temp.length; i++) {
				result += Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
			}
			return result.toUpperCase().substring(0, 16);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	    return result;
		
	}




}
